﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;


namespace PloobsEngine.SceneControl
{
    public class CircularGlowPostEffect : IPostEffect
    {
        #region IPostEffect Members
        private Effect glow;
        private Vector2 halfPixel;
        private Vector3[] positions;
        private float[] sizes;
        private float glowSize = 1;
        private EngineStuff engine;

        public void SetGlowObjects(IObject[] obj)
        {
            positions = new Vector3[obj.Length];
            sizes = new float[obj.Length];
            for (int i = 0; i < obj.Length; i++)
            {
                positions[i] = obj[i].Position;
                sizes[i] = obj[i].Modelo.GetBoudingSphere().Radius + glowSize;
            }
        }

        public void SetGlowSizeObjects(float[] sizes)
        {
            this.sizes = sizes;
        }

        public float GlowExtraSize
        {
            set
            {
                this.glowSize = value;
            }
            get
            {
                return glowSize;
            }
        }

        public void init(EngineStuff engine)
        {
            this.engine = engine;
            this.glow = EngineStuff.InternalContentManager.GetAsset<Effect>("glow");
            halfPixel.X = 0.5f / (float)EngineStuff.GraphicsDevice.PresentationParameters.BackBufferWidth;
            halfPixel.Y = 0.5f / (float)EngineStuff.GraphicsDevice.PresentationParameters.BackBufferHeight;
            
        }

        public void Draw(IRenderHelper rHelper, Microsoft.Xna.Framework.GameTime gt, Microsoft.Xna.Framework.Rectangle screenRec, IWorld world)
        {
                    glow.Parameters["cene"].SetValue(rHelper[PrincipalConstants.CombinedImage]);
                    glow.Parameters["objPos"].SetValue(positions);
                    glow.Parameters["num"].SetValue(positions.Length);
                    glow.Parameters["xViewProjection"].SetValue(world.ActiveCamera.View * world.ActiveCamera.Projection);
                    glow.Parameters["xCameraPos"].SetValue(world.ActiveCamera.Position);
                    glow.Parameters["halfPixel"].SetValue(halfPixel);
                    glow.Parameters["glowSize"].SetValue(sizes);
                    EngineStuff.GraphicsDevice.RenderState.DepthBufferWriteEnable = false;
                    EngineStuff.GraphicsDevice.RenderState.DepthBufferEnable = false;
                    rHelper.RenderTextureToFullScreenVertexPixel(glow);
                    EngineStuff.GraphicsDevice.RenderState.DepthBufferWriteEnable = true;
                    EngineStuff.GraphicsDevice.RenderState.DepthBufferEnable = true;
                        
        }
        #endregion
    }
}
